<?php
namespace Frieza\Modules\Push\Controllers;

use App\Http\Controllers\Controller;
use App\Pockets\GridDataModel as pGridDataModel;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Illuminate\Http\Request;
use Encore\Admin\Widgets\Tab;
use Encore\Admin\Widgets\Form;
use Illuminate\Support\MessageBag;
use Doraemon\model\PushTemplate;
use Doraemon\tools\Log;

class PushTemplateController extends Controller
{
    const PAGE_SIZE = 20;

    public function index(Request $request,Content $content) {
        $pageSize = $request->input("per_page", self::PAGE_SIZE);
        $page = $request->input("page", 1);

        $list = PushTemplate::getTplList($page, $pageSize);
        $gridModel = new pGridDataModel($list['list'], $list['total'], [
            'perPage' => $pageSize
        ]);
        $gridList = Admin::grid($gridModel,
            function (Grid $grid) use ($gridModel) {
                $grid->paginate($gridModel->getPerPage());
                $grid->disableRowSelector();
                $grid->disableColumnSelector();
                $grid->disableExport();
                $grid->disableFilter();

                $grid->column("id", 'ID');
                $grid->column("channel_id_hm", '华为 channelID');
                $grid->column("channel_id_xm", '小米 channelID');
                $grid->column("channel_id_op", 'OPPO channelID');
                $grid->column("channel_id_vo", 'VIVO channelID');
                $grid->column("channel_id_honor", '荣耀 channelID');
                // $grid->column("channel_id_third", "个推 channelID");
                $grid->column("description", '说明')->width(300);
                $grid->column("tpl_status", '状态')->display(function () {
                    return PushTemplate::TPL_STATUS_DESC[$this->tpl_status];
                })->label([
                    PushTemplate::TPL_STATUS_NORMAL => 'success',
                    PushTemplate::TPL_STATUS_DELETED => 'danger'
                ]);
                $grid->actions(function (Grid\Displayers\Actions $actions) {
                    $actions->disableDelete();
                });
            });

        $tab = new Tab();
        $tab->add('模板列表', $gridList->render());
        $content->row($tab);
        return $content
            ->header('推送通知模板列表')
            ->description(' ')
            ->breadcrumb(['text' => '推送通知模板列表']);
    }

    public function create(Request $request, Content $content, $id = 0)
    {
        $row = [];
        if ($id) {
            $row = PushTemplate::getRow(['id'=>$id]);
            $header = "编辑模板";
        } else {
            $header = "添加模板";
        }
        $tab = new Tab();
        $form = new Form($row);

        if ($id) {
            $form->hidden('id');
        }
        $form->text('channel_id_hm', '华为 channelID');
        $form->text('channel_id_xm', '小米 channelID');
        $form->text('channel_id_op', 'OPPO channelID');
        $form->text('channel_id_vo', 'VIVO channelID');
        $form->text('channel_id_honor', '荣耀 channelID');
        // $form->text('channel_id_third', '个推 channelID');
        $form->textarea('description', '说明')->rows(5)->required();
        $form->action(url('admin/push/pushTemplate/save'));

        $tab->addLink('模板列表', url('admin/push/pushTemplate'));
        $tab->add($header, $form->render(), true);
        return $content
            ->header('推送通知模板')
            ->description(' ')
            ->breadcrumb(['text' => $header])
            ->row($tab);
    }

    public function save(Request $request)
    {
        try {
            $params = $request->all();
            unset($params['_token']);

            $result = PushTemplate::saveTpl($params);
            if ($result) {
                $success = new MessageBag([
                    'title'   => '提交成功',
                ]);
                return redirect(url('/admin/push/pushTemplate'))->with(compact('success'));
            } else {
                $error = new MessageBag([
                    'title' => '操作失败'
                ]);
                return back()->withInput()->with(compact('error'));
            }
        } catch (\Exception $e) {
            Log::debug($e->getMessage());
            $error = new MessageBag([
                'title' => '操作失败,信息:' . $e->getMessage()
            ]);
            return back()->with(compact('error'));
        }
    }
}
